import type { FormRules } from 'element-plus'
import i18n from '../../i18n'

const formRules: FormRules = {
  product_id: [{ required: true, message: `${i18n.global.t('pleaseInput')}${i18n.global.t('productId')}`, trigger: 'change' }],
  loan_num_min: [{ required: true, message: `${i18n.global.t('pleaseInput')}${i18n.global.t('historyMinTimes')}`, trigger: 'blur' }],
  loan_num_max: [{ required: true, message: `${i18n.global.t('pleaseInput')}${i18n.global.t('historyMaxTimes')}`, trigger: 'blur' }],
  amount_max: [{ required: true, validator: createNumberValidator(`${i18n.global.t('maxAmount')}`), trigger: 'blur' }],
  amount_min: [{ required: true, validator: createNumberValidator(`${i18n.global.t('minAmount')}`), trigger: 'blur' }],
  amount_step: [{ required: true, validator: createNumberValidator(`${i18n.global.t('step')}`), trigger: 'blur' }],
  term: [{ required: true, validator: createNumberValidator(`${i18n.global.t('deadline')}`), trigger: 'blur' }],
  term_unit: [{ required: true, message: `${i18n.global.t('pleaseSelect')}${i18n.global.t('periodUnit')}`, trigger: 'change' }],
  total_period: [{ required: true, validator: createNumberValidator(`${i18n.global.t('periodCount')}`), trigger: 'blur' }],
  service_fee_rate: [
    { required: true, validator: createRateValidator(`${i18n.global.t('serviceFeeRate')}`), trigger: 'blur' }
  ],
  interest_rate: [
    { required: true, validator: createRateValidator(`${i18n.global.t('interestRate')}`), trigger: 'blur' }
  ],
  apply_prepay: [
    { required: true, message: `${i18n.global.t('pleaseSelect')}${i18n.global.t('isAllowPreRepay')}`, trigger: 'change' }
  ],
  // rollover_fee_rate: [
  //   { required: true, validator: createRateValidator('展期费率'), trigger: 'blur' }
  // ],
  data_cost: [
    { required: true, validator: createNumberValidator(`${i18n.global.t('dataFee')}`), trigger: 'blur' }
  ],
  rollover_day: [
    { required: true, validator: createNumberValidator(`${i18n.global.t('rolloverDayCount')}`), trigger: 'blur' }
  ],
  // pre_repay_charge_day: [
  //   { required: true, validator: createNumberValidator('还款前收费日'), trigger: 'blur' }
  // ],
  total_fee_rate: [
    { required: true, message: `${i18n.global.t('pleaseConfigTotalFee')}`, trigger: 'blur' }
  ],
  'overdue_interest_rate_config.overdue_day_min': [
    { required: true, validator: createNumberValidator(`${i18n.global.t('minDayCount')}`), trigger: 'blur' }
  ],
  'overdue_interest_rate_config.overdue_day_max': [
    { required: true, validator: createNumberValidator(`${i18n.global.t('maxDayCount')}`), trigger: 'blur' }
  ],
  'overdue_interest_rate_config.rate': [
    { required: true, validator: createRateValidator(`${i18n.global.t('everyDayInterestRate')}`), trigger: 'blur' }
  ],
  'prepay_fee_rate.pre_day_min': [
    { required: true, validator: createNumberValidator(`${i18n.global.t('minDayCount')}`), trigger: 'blur' }
  ],
  'prepay_fee_rate.pre_day_max': [
    { required: true, validator: createNumberValidator(`${i18n.global.t('maxDayCount')}`), trigger: 'blur' }
  ],
  'prepay_fee_rate.rate': [
    { required: true, validator: createRateValidator(`${i18n.global.t('interestRate')}`), trigger: 'blur' }
  ]
}

function createNumberValidator(message: string) {
  return function(rule: any, value: any, callback: any) {
    if (value === null || value === '') {
      callback(new Error(`${i18n.global.t('pleaseInput')}${message}`))
    } else if (!/^\d+$/.test(value)) {
      callback(new Error(`${message}${i18n.global.t('mustBePositiveInteger')}`))
    } else {
      const amountValue = parseInt(value, 10)
      if (amountValue < 0) {
        callback(new Error(`${message}${i18n.global.t('mustMoreThanEqualZero')}`))
      } else {
        callback()
      }
    }
  }
}

function createRateValidator(message: string) {
  return function(rule: any, value: any, callback: any) {
    if (value === null || value === '') {
      callback(new Error(`${i18n.global.t('pleaseInput')}${message}`))
    } else if (!/^\d{1,4}(\.\d{1,2})?$/.test(value)) { // 限制总位数不超过5位
      callback(new Error(`${message}${i18n.global.t('rateValidateMsg')}`))
    } else {
      const rateValue = parseFloat(value)
      if (rateValue < 0) {
        callback(new Error(`${message}${i18n.global.t('cannotNegative')}`))
      } else {
        callback()
      }
    }
  }
}

export const getPleaseInput = () => `${i18n.global.t('pleaseInput')}`

export {
  formRules,
  createNumberValidator,
  createRateValidator
}
